/*
 * engine.cpp
 *
 *  Created on: 30.06.2011
 *      Author: marcell
 */

#include <engine.h>
#include <sdlwindow.h>

#include <fs/filestd.h>

#include <asset/assetsystem.h>
#include <gfx/gl20/device.h>
#include <IL/il.h>
#include <IL/ilu.h>
#include <vfs.h>

ceEngine::ceEngine ()
	: _device (0)
  , _window (0)
  , _sharedValueManager(ceSharedValueManager::Get())
  , _mediaPath(std::string("."))
{
	_assetSystem = new ceAssetSystem (this);
}

bool ceEngine::ReadCommandLineParams (int argc, char** argv)
{
	for (int i=1; i<argc; i++)
		{
			if (strcmp (argv[i], "--media") == 0)
				{
					if (i >= argc) return false;
					SetMediaPath (std::string(argv[i+1]));
          ceVFS::Get()->SetRootPath(std::string(argv[i+1]));
					i++;
				}
		}
	return true;
}

void ceEngine::SetMediaPath (const std::string& mediaPath)
{
	_mediaPath = mediaPath;
}

bool ceEngine::Initialize (const Config& config, bool overrideWindowPresence)
{
  if (((!config.windowCreate && config.window == 0) || (config.windowCreate && config.window != 0)) && !overrideWindowPresence)
    {
      return false;
    }

  if (config.windowCreate)
    {
      ceSDLWindow* sdlwindow = new ceSDLWindow ();
      if (!sdlwindow->createWindow (config.windowWidth, config.windowHeight, 32, config.windowFullscreen))
        {
          if (!sdlwindow->createWindow (config.windowWidth, config.windowHeight, 24, config.windowFullscreen))
            {
              delete sdlwindow;
              return false;
            }
        }
      _window = sdlwindow;
    }
  else if (config.window)
    {
      _window = config.window;
    }
  else
    {
      _window = 0;
    }

  _device = new ceDeviceGL20 ();
  ilInit();
  iluInit();

  if (!_device->IsCapable ())
    {
      return false;
    }

  return true;
}
